<?php

namespace api\modules\v1\controllers;
use api\common\controllers\CommonController;
use yii\web\ServerErrorHttpException;
use yii\web\HttpException;
use api\modules\v1\models\Code;
use Yii;
use yii\web\NotFoundHttpException;
class RegController extends CommonController
{
    public $modelClass = 'api\modules\v1\models\User';


    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'],$actions['update'], $actions['create'], $actions['delete'], $actions['view']); //卸载操作
        return $actions;
    }

    /*
     * 注册账号
     * @return mixed
     */
    public function actionCreate()
     {
         $model = new $this->modelClass();
         $model->scenario='create';
         $model->attributes = Yii::$app->request->post();
         if ($model->signup()) {
             unset($model->password_hash);
             unset($model->auth_key);
             unset($model->password_hash);
             return $model;
         }elseif (!$model->hasErrors()) {
             throw new ServerErrorHttpException('由于不明原因未能创建对象.');
         }else{
             throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
         }
     }

    /*
     * 创建假账号
     */
    public function actionCreatetemp()
    {
        set_time_limit(0);
        $model = new $this->modelClass();
        $post = Yii::$app->request->post();
        $phones = $this->makePhone($post['phone_length'],$post['phone_num']);
        $pwd = 'chinajs123';
        $info = '';
        if(count($phones) >0 ){
            foreach($phones as $value){
                if(!$model->findOne(['phone'=>$value])){
                    $info .= $value .'----'.$pwd.PHP_EOL;
                    $model->signup_tmp($value,$pwd);
                }
            }
        }
        echo $info;exit;
    }

    /*
     * 随机生成手机号码
     * @param int $phone_length 手机号码长度
     * @param int $phone_num 手机号码数量
     */
    public function makePhone($phone_length=0,$phone_num=0){
        if($phone_length == 0 || $phone_num ==0){
            die("phone_length和phone_num 不能为空");
        }
        $arr = array(
            130,131,132,133,134,135,136,137,138,139,
            144,147,
            150,151,152,153,155,156,157,158,159,
            176,177,178,
            180,181,182,183,184,185,186,187,188,189,
        );
        $tmp= array();
        $phone_length -=3;
        for($i = 0; $i < $phone_num; $i++) {
            $num_start = (int)(str_pad('1',$phone_length,'0'));
            $num_end = (int)(str_pad('9',$phone_length,'9'));
            $tmp_phone = $arr[array_rand($arr)].mt_rand($num_start,$num_end);
            $tmp[] = $tmp_phone;
        }
        $tmp = array_unique($tmp);
        return $tmp;
    }

    /*
     * 忘记密码
     */
    public function actionForget(){
        $modelClass = $this->modelClass;
        $result = $model = $modelClass::findOne(['phone'=>Yii::$app->request->post('phone')]);
        if ($result === null) {
            throw new NotFoundHttpException('手机号码不存在.');
        }
        $model->scenario='forget';
        $model->attributes = Yii::$app->request->post();
        if ($model->forget()) {
            return [
                "message"=>"密码修改成功!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }
}
